<html>
<head><meta charset="utf-8"><title>fill match arms deletes _ case · t-compiler/rust-analyzer · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/index.html">t-compiler/rust-analyzer</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/fill.20match.20arms.20deletes.20_.20case.html">fill match arms deletes _ case</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="199604774"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/fill%20match%20arms%20deletes%20_%20case/near/199604774" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> dubi steinkek <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/fill.20match.20arms.20deletes.20_.20case.html#199604774">(Jun 03 2020 at 10:54)</a>:</h4>
<p>I'd like to try fixing this issue: <a href="https://github.com/rust-analyzer/rust-analyzer/issues/4165">https://github.com/rust-analyzer/rust-analyzer/issues/4165</a>.<br>
When using the <code>fill match arms</code> assist on</p>
<div class="codehilite"><pre><span></span><code><span class="k">enum</span> <span class="nc">E</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">A</span><span class="p">,</span><span class="w"> </span><span class="n">B</span><span class="p">,</span><span class="w"> </span><span class="n">C</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
<span class="c1">// ...</span>
<span class="k">match</span><span class="w"> </span><span class="n">e</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">E</span>::<span class="n">A</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="n">_</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="mi">2</span><span class="p">,</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>


<p>rust-analyzer replaces the _ with <code>E::B =&gt; {}, E::C =&gt; {}</code>.</p>
<p>As <code>tspiteri</code> mentions, there are 3 ways of handling the instead:</p>
<div class="codehilite"><pre><span></span><code>1.    E::B | E::C =&gt; 2,
2.    E::B =&gt; 2,
      E::C =&gt; 2,
3.    _ =&gt; 2
</code></pre></div>


<p>Which one should I be implementing?</p>



<a name="199604951"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/fill%20match%20arms%20deletes%20_%20case/near/199604951" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/fill.20match.20arms.20deletes.20_.20case.html#199604951">(Jun 03 2020 at 10:56)</a>:</h4>
<p>So, I think we should check if the <code>=&gt; expr</code> is a trival expression (there's some code for this in the assist already) and, if it is the case, we <em>should</em> remove the arm.</p>
<p>Otherwise, I think we should insert missing arms with <code>{}</code> before the wildcard</p>



<a name="199604975"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/fill%20match%20arms%20deletes%20_%20case/near/199604975" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/fill.20match.20arms.20deletes.20_.20case.html#199604975">(Jun 03 2020 at 10:56)</a>:</h4>
<p>so, </p>
<div class="codehilite"><pre><span></span><code><span class="k">match</span><span class="w"> </span><span class="n">e</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">  </span><span class="n">E</span>::<span class="n">A</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"></span>
<span class="w">  </span><span class="n">E</span>::<span class="n">B</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>
<span class="w">  </span><span class="n">E</span>::<span class="n">C</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>
<span class="w">  </span><span class="n">_</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="mi">2</span><span class="p">,</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="199605197"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/fill%20match%20arms%20deletes%20_%20case/near/199605197" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> dubi steinkek <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/fill.20match.20arms.20deletes.20_.20case.html#199605197">(Jun 03 2020 at 10:59)</a>:</h4>
<p>Why do you think we should remove the arm? That is not what I would be expecting as a user.</p>



<a name="199605584"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/fill%20match%20arms%20deletes%20_%20case/near/199605584" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/fill.20match.20arms.20deletes.20_.20case.html#199605584">(Jun 03 2020 at 11:03)</a>:</h4>
<p>If the arm is trivial, I would expect it to be removed. What else woudl you do with a trivial arm, if you are going to fill others anyway?</p>



<a name="199605796"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/fill%20match%20arms%20deletes%20_%20case/near/199605796" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> dubi steinkek <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/fill.20match.20arms.20deletes.20_.20case.html#199605796">(Jun 03 2020 at 11:05)</a>:</h4>
<p>Maybe you know there is only one match arm left, and want to have an explicit match arm instead of a placeholder</p>



<a name="199605876"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/fill%20match%20arms%20deletes%20_%20case/near/199605876" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> dubi steinkek <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/fill.20match.20arms.20deletes.20_.20case.html#199605876">(Jun 03 2020 at 11:06)</a>:</h4>
<p>i.e. replace</p>
<div class="codehilite"><pre><span></span><code><span class="k">match</span><span class="w"> </span><span class="n">some_option</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="nb">Some</span><span class="p">(</span><span class="n">v</span><span class="p">)</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="n">v</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="n">_</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="n">panic</span><span class="o">!</span><span class="p">(),</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>


<p>with </p>
<div class="codehilite"><pre><span></span><code><span class="k">match</span><span class="w"> </span><span class="n">some_option</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="nb">Some</span><span class="p">(</span><span class="n">v</span><span class="p">)</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="n">v</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="nb">None</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="n">panic</span><span class="o">!</span><span class="p">(),</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="199605975"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/fill%20match%20arms%20deletes%20_%20case/near/199605975" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/fill.20match.20arms.20deletes.20_.20case.html#199605975">(Jun 03 2020 at 11:07)</a>:</h4>
<p>I'd say if the has <em>jsut</em> panic, it's fine to remove it. It has something else, its not trivial and won't be remoed</p>



<a name="199606593"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/fill%20match%20arms%20deletes%20_%20case/near/199606593" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> dubi steinkek <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/fill.20match.20arms.20deletes.20_.20case.html#199606593">(Jun 03 2020 at 11:16)</a>:</h4>
<p>okay, I see. You said there is some code for checking whether an expression is trivial but I can't find it, what exactly should I look for?</p>



<a name="199606608"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/fill%20match%20arms%20deletes%20_%20case/near/199606608" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> dubi steinkek <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/fill.20match.20arms.20deletes.20_.20case.html#199606608">(Jun 03 2020 at 11:17)</a>:</h4>
<p>or to determine whether the code is trivial should I just check whether the expression is a block or not?</p>



<a name="199606677"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/fill%20match%20arms%20deletes%20_%20case/near/199606677" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/fill.20match.20arms.20deletes.20_.20case.html#199606677">(Jun 03 2020 at 11:18)</a>:</h4>
<p><a href="https://github.com/rust-analyzer/rust-analyzer/blob/7e2bca4ec356655fe47d50b466c34c34e7cae565/crates/ra_assists/src/handlers/fill_match_arms.rs#L77-L82">https://github.com/rust-analyzer/rust-analyzer/blob/7e2bca4ec356655fe47d50b466c34c34e7cae565/crates/ra_assists/src/handlers/fill_match_arms.rs#L77-L82</a></p>



<a name="199606702"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/fill%20match%20arms%20deletes%20_%20case/near/199606702" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/fill.20match.20arms.20deletes.20_.20case.html#199606702">(Jun 03 2020 at 11:18)</a>:</h4>
<p>Looks like I am remembering incorrectly -- it doesn't try to check if the expression to the right of <code>=&gt;</code> is "trivial"</p>



<a name="199607208"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/fill%20match%20arms%20deletes%20_%20case/near/199607208" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> dubi steinkek <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/fill.20match.20arms.20deletes.20_.20case.html#199607208">(Jun 03 2020 at 11:25)</a>:</h4>
<p>It looks like <code>is_trivial</code> was removed in <a href="https://github.com/rust-analyzer/rust-analyzer/commit/6087c014608108e2b971608e214a74759743e95e">https://github.com/rust-analyzer/rust-analyzer/commit/6087c014608108e2b971608e214a74759743e95e</a>. If I understand correctly an expression is not trivial if it is wrapped in block, right? So that shouln't be too hard to check.</p>



<a name="199608166"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/fill%20match%20arms%20deletes%20_%20case/near/199608166" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> dubi steinkek <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/fill.20match.20arms.20deletes.20_.20case.html#199608166">(Jun 03 2020 at 11:38)</a>:</h4>
<div class="codehilite"><pre><span></span><code><span class="k">fn</span> <span class="nf">expression_is_trivial</span><span class="p">(</span><span class="n">expr</span>: <span class="kp">&amp;</span><span class="nc">ast</span>::<span class="n">Expr</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="kt">bool</span> <span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">match</span><span class="w"> </span><span class="n">expr</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="n">ast</span>::<span class="n">Expr</span>::<span class="n">ArrayExpr</span><span class="p">(</span><span class="n">_</span><span class="p">)</span><span class="w"></span>
<span class="w">        </span><span class="o">|</span><span class="w"> </span><span class="n">ast</span>::<span class="n">Expr</span>::<span class="n">ReturnExpr</span><span class="p">(</span><span class="n">_</span><span class="p">)</span><span class="w"></span>
<span class="w">        </span><span class="o">|</span><span class="w"> </span><span class="n">ast</span>::<span class="n">Expr</span>::<span class="n">CallExpr</span><span class="p">(</span><span class="n">_</span><span class="p">)</span><span class="w"></span>
<span class="w">        </span><span class="o">|</span><span class="w"> </span><span class="n">ast</span>::<span class="n">Expr</span>::<span class="n">MethodCallExpr</span><span class="p">(</span><span class="n">_</span><span class="p">)</span><span class="w"></span>
<span class="w">        </span><span class="o">|</span><span class="w"> </span><span class="n">ast</span>::<span class="n">Expr</span>::<span class="n">RangeExpr</span><span class="p">(</span><span class="n">_</span><span class="p">)</span><span class="w"></span>
<span class="w">        </span><span class="o">|</span><span class="w"> </span><span class="n">ast</span>::<span class="n">Expr</span>::<span class="n">MacroCall</span><span class="p">(</span><span class="n">_</span><span class="p">)</span><span class="w"></span>
<span class="w">        </span><span class="o">|</span><span class="w"> </span><span class="n">ast</span>::<span class="n">Expr</span>::<span class="n">Literal</span><span class="p">(</span><span class="n">_</span><span class="p">)</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="kc">true</span><span class="p">,</span><span class="w"></span>
<span class="w">        </span><span class="n">ast</span>::<span class="n">Expr</span>::<span class="n">TupleExpr</span><span class="p">(</span><span class="n">e</span><span class="p">)</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="n">e</span><span class="p">.</span><span class="n">exprs</span><span class="p">().</span><span class="n">all</span><span class="p">(</span><span class="o">|</span><span class="n">e</span><span class="o">|</span><span class="w"> </span><span class="n">expression_is_trivial</span><span class="p">(</span><span class="o">&amp;</span><span class="n">e</span><span class="p">)),</span><span class="w"></span>
<span class="w">        </span><span class="n">ast</span>::<span class="n">Expr</span>::<span class="n">BoxExpr</span><span class="p">(</span><span class="n">e</span><span class="p">)</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="n">e</span><span class="p">.</span><span class="n">expr</span><span class="p">().</span><span class="n">map_or</span><span class="p">(</span><span class="kc">false</span><span class="p">,</span><span class="w"> </span><span class="o">|</span><span class="n">e</span><span class="o">|</span><span class="w"> </span><span class="n">expression_is_trivial</span><span class="p">(</span><span class="o">&amp;</span><span class="n">e</span><span class="p">)),</span><span class="w"></span>
<span class="w">        </span><span class="n">ast</span>::<span class="n">Expr</span>::<span class="n">RefExpr</span><span class="p">(</span><span class="n">e</span><span class="p">)</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="n">e</span><span class="p">.</span><span class="n">expr</span><span class="p">().</span><span class="n">map_or</span><span class="p">(</span><span class="kc">false</span><span class="p">,</span><span class="w"> </span><span class="o">|</span><span class="n">e</span><span class="o">|</span><span class="w"> </span><span class="n">expression_is_trivial</span><span class="p">(</span><span class="o">&amp;</span><span class="n">e</span><span class="p">)),</span><span class="w"></span>
<span class="w">        </span><span class="n">ast</span>::<span class="n">Expr</span>::<span class="n">PrefixExpr</span><span class="p">(</span><span class="n">e</span><span class="p">)</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="n">e</span><span class="p">.</span><span class="n">expr</span><span class="p">().</span><span class="n">map_or</span><span class="p">(</span><span class="kc">false</span><span class="p">,</span><span class="w"> </span><span class="o">|</span><span class="n">e</span><span class="o">|</span><span class="w"> </span><span class="n">expression_is_trivial</span><span class="p">(</span><span class="o">&amp;</span><span class="n">e</span><span class="p">)),</span><span class="w"></span>
<span class="w">        </span><span class="n">ast</span>::<span class="n">Expr</span>::<span class="n">CastExpr</span><span class="p">(</span><span class="n">e</span><span class="p">)</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="n">e</span><span class="p">.</span><span class="n">expr</span><span class="p">().</span><span class="n">map_or</span><span class="p">(</span><span class="kc">false</span><span class="p">,</span><span class="w"> </span><span class="o">|</span><span class="n">e</span><span class="o">|</span><span class="w"> </span><span class="n">expression_is_trivial</span><span class="p">(</span><span class="o">&amp;</span><span class="n">e</span><span class="p">)),</span><span class="w"></span>
<span class="w">        </span><span class="n">_</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="kc">false</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>


<p>how about this?</p>



<a name="199608256"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/fill%20match%20arms%20deletes%20_%20case/near/199608256" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/fill.20match.20arms.20deletes.20_.20case.html#199608256">(Jun 03 2020 at 11:39)</a>:</h4>
<p>I'd maybe just used <code>expr.text() == "{}" || expr.text() == "()" || expr.text() == "todo!()"</code>, should be good enoguh</p>



<a name="199608323"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/fill%20match%20arms%20deletes%20_%20case/near/199608323" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> dubi steinkek <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/fill.20match.20arms.20deletes.20_.20case.html#199608323">(Jun 03 2020 at 11:40)</a>:</h4>
<p>okay <span aria-label="thumbs up" class="emoji emoji-1f44d" role="img" title="thumbs up">:thumbs_up:</span></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>